package net.oov.identify;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.SimpleAnalyzer;
import org.apache.lucene.analysis.Token;
import org.apache.lucene.analysis.TokenStream;


/**
 * 
 * 串频容器实例工厂
 * @author huangsui.cn
 *
 */
public class SequenceFreqContainerFactory {
	
	public static final int CHARSEQUNENCE_STYLE_HANDLED_SPECIALCHAR_WHOLEFLAG_AND_NGRAMSEG = 1; 
	
	private List<String> rawTextPathIn = null;//the path of file saved raw data
	private String charEncoding = null;//the char encoding of file saved raw data
	
	Analyzer an = null;
	
	{
		rawTextPathIn = new ArrayList<String>();
		rawTextPathIn.add("txt/in_raw_50025174.txt");
		
		charEncoding = "UTF-8";
		
		an = new SimpleAnalyzer();
	}
	
	public SequenceFreqContainerFactory(){
		super();
	}
	
	public SequenceFreqContainerFactory(List<String> rawTextPathIn){
		super();
		this.rawTextPathIn = rawTextPathIn;
	}
	
	public SequenceFreqContainer createNewContainer(List<String> rawTextPathIn, Analyzer an, int loadMethod, int maxSeqLengthAllowed){

		SequenceFreqContainer sequenceFreqContainer = new SequenceFreqContainer(loadMethod,maxSeqLengthAllowed);
		try {
			for(String rawTextPath:rawTextPathIn){
				InputStreamReader isr = new InputStreamReader(new FileInputStream(rawTextPath), charEncoding);
				TokenStream ts = an.tokenStream("product name", isr);
				addRawCharSequence(sequenceFreqContainer, ts);
				ts.close();
				isr.close();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 

		return sequenceFreqContainer;
	}
	
	
	/**
	 * 将ts的数据，经过词典过滤和邻接检查，对处理后的字串进行统计，并加入到串频容器中。
	 * 期间会经过分词处理，词典的词被过滤
	 * 特殊字符邻接的串被进行特殊标记，"-"标示邻接无意义特殊字符，"+"标示邻接有意义特殊字符。
	 * @param sfc
	 * @param br
	 */
	public void addRawCharSequence(SequenceFreqContainer sfc, TokenStream ts){
		
		SequenceSawInsert ssi = new SequenceSawInsertIns();
		try {
			Token tkPre = null;
			Token tk = null;
			Token tkNext = new Token();
			tk = ts.next(tkNext);
			
			while(tk != null){
				tkNext = ts.next(tkNext);
				ssi.addHandedCharSequence(sfc,tkPre, tk, tkNext);
				tkPre = tk;
				tk = tkNext;
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	
	
	/**
	 * replace chars which are not cjk,letter and digit to whitespace
	 * @param str
	 * @return
	 */
	public String specialCharFilter(String str){
		
		if(str == null)return null;
		String ret = str;
		ret = ret.replaceAll("([^\u4e00-\u9fa5a-zA-Z0-9])+", " ");//not chinese and letter and digit replace
		ret = ret.replaceAll("[a-zA-Z0-9]+\\s", " ");//the end char is letter or digit,handle away
		ret = ret.replaceAll("[a-zA-Z0-9]{2,}", " ");//long letter and digit replace
		char c = ret.charAt(ret.length()-1);
		if((c>'a'&&c<'z')||(c>'A'&&c<'Z')||Character.isDigit(c))ret = ret.substring(0,ret.length()-1);
		Pattern p = Pattern.compile("^[\\s]*[a-zA-Z0-9]+[\\s]*$");//only letter and digit replace
		Matcher m = p.matcher(ret);
		if(m.find()){
			return "";
		}
		return ret;
		
	}

	
}
